﻿using UnityEngine;
using System.Collections;

public partial class GameSystem : MonoBehaviour 
{

	public void LoadScene( string strSceneName )
	{
		Application.LoadLevel (strSceneName);
	}

    public void GameEnding()
    {
        if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().m_fChaos < 60)
        {
            Application.LoadLevel("Ending_1");
        }
        else if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().m_fChaos < 30)
        {
            Application.LoadLevel("Ending_0");
        }
    }

    public void DeleteThisUnit(UnitStatus ObjStatus, bool isFriend = true ,bool bJustHidden = false)
    {
        GameObject DeleteObj = null;

        if (isFriend)
        {
            for (int i = 0; i < GameObject.Find("Friends").transform.childCount; ++i)
            {
                if (DeleteObj == null)
                {
                    for (int j = 0; j < GameObject.Find("Friends").transform.GetChild(i).childCount; ++j)
                    {
                        if (GameObject.Find("Friends").transform.GetChild(i).GetChild(j).FindChild("Main").FindChild("Unit").GetComponent<UnitStatus>() == ObjStatus)
                        {
                            DeleteObj = GameObject.Find("Friends").transform.GetChild(i).GetChild(j).gameObject;
                            break;
                        }
                    }
                }
                else
                    break;
            }
        }
        else
        {
            for (int i = 0; i < GameObject.Find("Enemies").transform.childCount; ++i)
            {
                if (GameObject.Find("Enemies").transform.GetChild(i).FindChild("Main").FindChild("Unit").GetComponent<UnitStatus>() == ObjStatus)
                {
                    DeleteObj = GameObject.Find("Enemies").transform.GetChild(i).gameObject;
                    break;
                }
            }
        }

        if (isFriend)
        {
            if (DeleteObj != null)
            {
                for (int i = 0; i < GameObject.Find("NameUI").transform.childCount; ++i)
                {
                    if (GameObject.Find("NameUI").transform.GetChild(i).GetComponent<UIFollowTarget>().target == DeleteObj.transform.FindChild("Main").FindChild("Unit").transform)
                    {
                        if (!bJustHidden)
                            Destroy(GameObject.Find("NameUI").transform.GetChild(i).gameObject);
                        else
                            GameObject.Find("NameUI").transform.GetChild(i).gameObject.SetActive(false);
                        break;
                    }
                }
            }
            else
                Debug.LogError("Cant Find this unit!!");
        }


        StartCoroutine(DeleteThisUnit_Coroutine(DeleteObj, bJustHidden, isFriend));
    }

    IEnumerator DeleteThisUnit_Coroutine(GameObject DelObj, bool bJustHidden, bool isFriend)
    {
        do
        {
            yield return null;

            DelObj.transform.FindChild("Main").FindChild("Unit").GetComponent<SpriteRenderer>().color = new Color(1,1,1, DelObj.transform.FindChild("Main").FindChild("Unit").GetComponent<SpriteRenderer>().color.a - (1.25f * Time.deltaTime));

        } while (DelObj.transform.FindChild("Main").FindChild("Unit").GetComponent<SpriteRenderer>().color.a > 0);

        GameObject.Find("DebugTile").GetComponent<Background>().m_ListTile[DelObj.transform.FindChild("Main").FindChild("Unit").GetComponent<CommonFSM>().m_iPrevTIleIdx].GetComponent<Tile>().m_iOption = DelObj.transform.FindChild("Main").FindChild("Unit").GetComponent<CommonFSM>().m_iPrevTileOption;

        if (!bJustHidden)
        {
            if (isFriend)
            {
                for (int i = 0; i < GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().UnitList.Count; ++i)
                {
                    if (DelObj.GetComponent<UnitStatus>() == GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().UnitList[i])
                        GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().UnitList.RemoveAt(i);
                }

                GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().GraveList.Add(DelObj.GetComponent<UnitStatus>());
            }

            DestroyImmediate(DelObj);
        }
        else
            DelObj.gameObject.SetActive(false);
    }
}

//SingleTon
public partial class GameSystem : MonoBehaviour {
    private static GameSystem instance;
    private static GameObject container;

    public static GameSystem GetInstance()
    {
        if (!instance)
        {
            container = new GameObject();
            container.name = "ResourceMgr";
            instance = container.AddComponent(typeof(GameSystem)) as GameSystem;
        }
        return instance;
    }
}
